package com.zhenxiang.juc.blockingqueue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @Author : James
 * @Create : 2021-12-22-17:40
 * @Description : 阻塞队列
 */
public class blockingQueueDemo {

    public static void main(String[] args) throws InterruptedException {
        test04();
    }

    /**
     * 往队列添加、移除元素
     * 会抛出异常
     */
    public static void test1(){
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue(3);
        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("b"));
        System.out.println(blockingQueue.add("c"));
        // 抛出 java.lang.IllegalStateException: Queue full 队列已满异常
       //  System.out.println(blockingQueue.add("d"));
        System.out.println("==========================");
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        // 抛出 java.util.NoSuchElementException
        // System.out.println(blockingQueue.remove());
    }

    /**
     * 有返回值 没有异常
     */
    public static void test02(){
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));
        // 超过容量 不会抛出异常 会返回false
        System.out.println(blockingQueue.offer("d"));

        System.out.println("=================================");
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        // 删除不存在的元素 不会抛出异常 会返回null
        // System.out.println(blockingQueue.poll());
    }

    /**
     * 等待 阻塞（一直阻塞）
     */
    public static void  test03() throws InterruptedException {
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
        blockingQueue.put("a");
        blockingQueue.put("b");
        blockingQueue.put("c");
        /**队列没有空间位置了，就会在这一直 阻塞 **/
        //blockingQueue.put("d");
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        /**队列中没有元素 会一直等待**/
        //System.out.println(blockingQueue.take());
    }

    /**
     * 等待 阻塞 （超时等待）
     */
    public static void test04() throws InterruptedException {

        ArrayBlockingQueue  blockingQueue = new ArrayBlockingQueue(3);
        blockingQueue.offer("a");
        blockingQueue.offer("b");
        blockingQueue.offer("c");
        // 等待 超过两秒就退出
        blockingQueue.offer("d",2, TimeUnit.SECONDS);

        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        // 等待 超过两秒就退出
        System.out.println(blockingQueue.poll());
    }



}



